<?php
/**
 * Author: Administrator
 * Date: 2022/7/16 1:29
 */

namespace DesignPatterns\Creation\Pool;


use function PHPUnit\Framework\isInstanceOf;

class Pool
{
    protected $class;
    protected $instances = [];

    public function __construct($class)
    {
        $this->class = $class;
    }

    public function get() {
        if (count($this->instances) > 0) {
            return array_shift($this->instances);
        }
        return new $this->class();
    }

    public function depose($instance): void
    {
        if ($instance instanceof $this->class) {
            $this->instances[] = $instance;
        } else {
            throw new \InvalidArgumentException(get_class($instance).' is not instance of '.$this->class);
        }
    }

}